<template>
  <div>
    <a-drawer title="课次详情" placement="right" :visible="visible" @close="visible=false" width="740">
      <a-spin :spinning="loading">
        <div class="classTitle">
          <span class="text" v-if="defaultValue.course">{{defaultValue.class_name||'-'}}</span>
          <span>
            <a-button
              type="primary"
              style="margin-right:10px;"
              v-if="defaultValue.type=='schedule'"
              @click="clickEdit"
            >编辑课次</a-button>
            <a-button v-if="defaultValue.type=='schedule'" @click="del">删除课次</a-button>
          </span>
        </div>
        <div class="stepTitle">
          <div class="title">课次信息</div>
          <a-row style="padding:0 40px;">
            <a-col :span="12" style="margin-top:10px;" v-if="structure_name">
              <span class="label">所在校区：</span>
              <span class="label_content">{{structure_name}}</span>
            </a-col>
            <a-col :span="12" style="margin-top:10px;">
              <span class="label">上课时间：</span>
              <span
                class="label_content"
              >{{defaultValue.class_start_time}}~{{defaultValue.class_end_time}}</span>
            </a-col>
            <a-col :span="12" style="margin-top:10px;">
              <span class="label">授课课程：</span>
              <span
                class="label_content"
                v-if="defaultValue.course&&defaultValue.course.name"
              >{{defaultValue.course.name||'-'}}</span>
              <span v-else>{{defaultValue.course||'-'}}</span>
            </a-col>

            <!-- <a-col :span="12" style="margin-top:10px;">
              <span class="label">上课班级：</span>
              <span class="label_content">{{defaultValue.class_name||'--'}}</span>
            </a-col>-->
            <a-col :span="12" style="margin-top:10px;">
              <span class="label">上课教室：</span>
              <span class="label_content">{{defaultValue.classroom||'未设置'}}</span>
            </a-col>
            <a-col :span="24" style="margin-top:10px;">
              <span class="label">上课老师：</span>
              <span
                class="label_content"
                v-if="defaultValue.teachers&&defaultValue.teachers.length>0"
              >
                <span v-for="(item,index) in defaultValue.teachers" :key="index">
                  {{item}}
                  <span v-if="index!==defaultValue.teachers.length-1">，</span>
                </span>
              </span>
              <span class="label_content" v-else>暂无老师</span>
            </a-col>
            <a-col :span="24" style="margin-top:10px;">
              <span class="label">上课内容：</span>
              <span
                class="label_content"
                style="display:inline-block;"
              >{{defaultValue.content||'--'}}</span>
            </a-col>
          </a-row>
        </div>
        <div class="stepTitle">
          <div class="title" v-if="defaultValue.type=='schedule'">上课学员</div>
          <p></p>
          <a-button
            size="small"
            v-if="defaultValue.type=='schedule'&&scheduleType!=='1v1'"
            @click="getLinshiStudent(id)"
          >添加临时学员</a-button>
          <div style="margin:10px 0;">共&nbsp;{{studentList.length}}&nbsp;名学员</div>
          <div v-if="defaultValue.type=='call'">
            <a-table
              :columns="calledStudentColumns"
              :data-source="studentList"
              :pagination="false"
              class="user_management_table"
              style="border:1px solid #e8e8e8;"
            >
              <span slot="name" slot-scope="text,record">
                <!-- <span v-if="record.student">{{record.student.name}}</span> -->
                <span
                  :title="(record.name||'-')+(record.student_type==1?'(临时学员)':record.student_type==2?'(调课学员)':record.student_type==3?'(试听学员)':'')"
                >{{record.name||'-'}}{{record.student_type==1?'(临时学员)':record.student_type==2?'(调课学员)':record.student_type==3?'(试听学员)':''}}</span>
              </span>
              <!-- 已点名 -->
              <span slot="call_type" slot-scope="text,record">
                <span v-if="record.call_type==1" style="color:rgb(153, 153, 153)">到课</span>
                <span v-else-if="record.call_type==2" style="color:rgb(153, 153, 153)">迟到</span>
                <span v-else-if="record.call_type==3" style="color: rgb(88, 183, 239);">请假</span>
                <span v-else-if="record.call_type==4" style="color:rgb(255, 103, 103)">未到</span>
                <span v-else style="color:rgb(153, 153, 153)">--</span>
              </span>
              <span slot="phone" slot-scope="text,record">
                <span>{{record.main_phone||'--'}}</span>
              </span>
              <span slot="deduction" slot-scope="text,record">
                <span v-if="record.deduction&&record.deduction.type">
                  <span
                    v-if="record.deduction.type=='hour'"
                  >{{+Number(record.deduction.allowance)+'课时'}}</span>
                  <span v-else-if="record.deduction.type=='day'">按天自动消耗</span>
                  <span v-else-if="record.deduction.type=='super'">按天自动消耗（本次超上）</span>
                  <span v-else-if="record.deduction.type=='audition'">试听不扣课时</span>
                </span>
                <span v-else>-</span>
              </span>
              <span slot="remarks" slot-scope="text,record">{{record.remarks||'--'}}</span>
            </a-table>
          </div>
          <div v-else>
            <a-table
              :columns="studentColumns"
              :dataSource="studentList"
              :pagination="false"
              class="user_management_table"
              style="border:1px solid #e8e8e8;"
            >
              <span slot="name" slot-scope="text,record">
                <span
                  :title="(record.name||'-')+(record.student_type==1?'(临时学员)':record.student_type==2?'(调课学员)':record.student_type==3?'(试听学员)':'')"
                >{{record.name}}{{record.student_type==1?'(临时学员)':record.student_type==2?'(调课学员)':record.student_type==3?'(试听学员)':''}}</span>
                <!-- {{record.student_type==0?'':record.student_type==1?'(临时学员)':record.student_type==2?'(调课学员)':record.student_type==3?'(试听学员)':''}} -->
              </span>
              <!-- 未点名 -->
              <span slot="action" slot-scope="text,record">
                <span v-if="record.student_type!==3">
                  <a
                    v-if="record.pivot_id"
                    :disabled="scheduleType=='1v1'"
                    @click="remove(record)"
                  >移出</a>
                  <a v-else :disabled="scheduleType=='1v1'" @click="showChangeClass(record)">调课</a>
                </span>
                <span v-else>--</span>
              </span>
              <span slot="class_hour" slot-scope="text,record">
                <span v-if="record.deduction==1">--</span>
                <span v-else>
                  <span>{{record.class_hour}}</span>
                  <span v-if="record.type==1">天</span>
                  <span v-if="record.type==2">课时</span>
                </span>
              </span>
              <span slot="deduct" slot-scope="text,record">{{record.class_hour||'--'}}</span>
              <div slot="surplusClassHour" slot-scope="text,record">
                <div v-if="record.surplus_clas_hour">
                  <div
                    v-if="record.surplus_clas_hour.hour||+Number(record.surplus_clas_hour.hour)==0"
                  >{{record.surplus_clas_hour.hour}}课时</div>
                </div>
                <div
                  v-if="(record.surplus_clas_hour.hour||+Number(record.surplus_clas_hour.hour)==0)&&(record.surplus_clas_hour.day||Number(record.surplus_clas_hour.day)==0)"
                  style="margin:5px 0;"
                ></div>
                <div
                  v-if="record.surplus_clas_hour.day||Number(record.surplus_clas_hour.day)==0"
                >{{record.surplus_clas_hour.day}}天</div>
              </div>
            </a-table>
          </div>
        </div>
      </a-spin>
    </a-drawer>
    <!-- 编辑 -->
    <a-modal
      title="编辑课次信息"
      :visible="editModal"
      @cancel="editModal=false"
      @ok="handleOk('changeClassFormModal')"
      :confirmLoading="modalLoading"
      okText="保存"
    >
      <a-form-model
        ref="changeClassFormModal"
        :model="changeClassForm"
        :rules="changeClassRules"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <a-form-model-item label="上课日期" prop="start_time">
          <a-date-picker
            placeholder="请选择上课日期"
            v-model="changeClassForm.start_time"
            style="width: 180px;"
          />
        </a-form-model-item>
        <a-form-model-item label="上课时间" prop="class_time_id">
          <a-select
            placeholder="请选择上课时间"
            style="width: 180px"
            v-model="changeClassForm.class_time_id"
          >
            <a-select-option
              v-for="(item,index) in classTimeSelect"
              :key="index"
              :value="item.id"
            >{{item.start_time}}&nbsp;~&nbsp;{{item.end_time}}</a-select-option>
          </a-select>
          <a @click="$refs.classTime.showModal()" style="margin-left:10px;">设置</a>
        </a-form-model-item>
        <a-form-model-item label="上课老师">
          <a-select
            placeholder="请选择上课老师"
            style="width: 300px"
            v-model="changeClassForm.teacher_ids"
            mode="multiple"
          >
            <a-select-option
              v-for="(item,index) in teacherSelect"
              :key="index"
              :value="item.id"
            >{{item.name}}</a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="授课课程">
          <span v-if="changeClassForm.course">{{changeClassForm.course.name||'-'}}</span>
          <span v-else>-</span>
        </a-form-model-item>
        <a-form-model-item label="上课教室">
          <a-select
            v-model="changeClassForm.classroom_id"
            placeholder="请选择上课教室"
            style="width:180px;margin-right:10px;"
          >
            <a-select-option :value="null">不指定</a-select-option>
            <a-select-option
              v-for="(item,index) in classroomList"
              :key="index"
              :value="item.id"
            >{{item.name}}</a-select-option>
          </a-select>
          <a href="javascript:;" @click="$refs.classRoom.showModal()">设置</a>
        </a-form-model-item>
        <a-form-model-item label="上课内容">
          <a-input v-model="changeClassForm.content" type="textarea" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
    <!-- 临时学员列表 -->
    <a-modal
      title="临时学员列表"
      :visible="linshiStudentModel"
      :width="860"
      okText="选好了"
      @ok="addStudent"
      @cancel="linshiStudentModel = false"
    >
      <a-input
        style="width:280px;margin-bottom:10px;"
        v-model="linshiKeywords"
        placeholder="请输入学员姓名/手机号(回车搜索)"
        @pressEnter="getLinshiStudent(id)"
      >
        <a-tooltip slot="suffix" title="搜索学生姓名/手机号">
          <a-icon type="search" style="color: rgba(0,0,0,.45)" @click="getLinshiStudent(id)" />
        </a-tooltip>
      </a-input>
      <a-table
        :columns="linshiColumns"
        :data-source="linshiData"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :pagination="false"
        class="user_management_table"
        style="border:1px solid #e8e8e8;max-height:400px;overflow-y:auto"
      >
        <span slot="courseName" slot-scope="text,record">{{record.course_name}}</span>
        <span slot="remain" slot-scope="text,record">
          <span v-if="record.surplus_clas_hour">
            <span
              v-if="record.surplus_clas_hour.hour||Number(record.surplus_clas_hour.hour)==0"
            >{{record.surplus_clas_hour.hour}}课时</span>
            <span v-if="record.surplus_clas_hour.day||Number(record.surplus_clas_hour.day)==0">
              <span v-if="record.surplus_clas_hour.hour||Number(record.surplus_clas_hour.hour)==0">/</span>
              {{record.surplus_clas_hour.day}}天
            </span>
          </span>
          <span v-else>--</span>
        </span>
      </a-table>
    </a-modal>
    <a-modal
      title="选择调入的课次"
      :visible="changeClassModel"
      okText="选好了"
      :width="1344"
      @ok="changeClass"
      @cancel="changeClassModel = false"
    >
      <a-button @click="last7day(changeClassNowDate)">上一周</a-button>
      <!-- <a-date-picker
        v-model="changeClassNowDate"
        @change="changeNowDate"
        
        :allowClear="false"
      />-->
      <a-button @click="getNowWeek()" style="margin:0 10px 10px;">本周</a-button>
      <a-button @click="next7day(changeClassNowDate)">下一周</a-button>

      <a-table
        class="user_management_table"
        style="border:1px solid #e8e8e8;max-height:400px;overflow-y:auto"
        :columns="changeClassColumns"
        :data-source="changeClassLoadData"
        :pagination="false"
        :row-selection="changeClassRowSelection"
      >
        <span slot="name" slot-scope="text,record">{{record.class_name}}</span>
        <span slot="course" slot-scope="text,record">{{record.course_name}}</span>
        <span slot="time" slot-scope="text,record">
          {{record.date|dayjs}}
          <span
            v-if="record.class_start_time||record.class_end_time"
          >&nbsp;{{record.class_start_time}}~{{record.class_end_time}}</span>
        </span>
        <span slot="classroom" slot-scope="text,record">{{record.class_room_name||'--'}}</span>
        <span slot="teacher" slot-scope="text,record">
          <span v-if="record.teachers.length>0">
            <span v-for="(item,index) in record.teachers" :key="index">
              {{item}}
              <span v-if="index!==record.teachers.length-1">，</span>
            </span>
          </span>
          <span v-else></span>
        </span>
        <span slot="peopleNum" slot-scope="text,record">{{record.schedule_student.length}}</span>
      </a-table>
    </a-modal>
    <classroom-components ref="classRoom" @reload="getSelect"></classroom-components>
    <class-time ref="classTime" @reload="getSelect"></class-time>
  </div>
</template>
<script>
import {
  newTakeNameInfo,
  takeNameLinshiStudent,
  delAdjust,
  mixStudents,
  addLinshiStudentList,
  removeStudent,
  changeClassTable,
  sureChangeClass,
} from '@/api/takeName'
import { lessonInfo } from '@/api/lessonTable'
//下拉菜单
import { classroomList } from '@/api/classroom'
import { classTimeList } from '@/api/classTime'
import { teacherList } from '@/api/teacher'
import classTime from '@/components/classTimeModel'
import classroomComponents from '@/components/classroomManage'
import moment from 'moment'
export default {
  name: 'lessonDrawer',
  components: {
    classTime,
    classroomComponents,
  },
  props: ['structure_name'],
  data() {
    return {
      scheduleType: null, // 判断是1对1还是1对N
      loading: false,
      visible: false,
      // 主要信息
      id: null, //课次ID
      type: null,
      time: '',
      class_id: null,
      date_Monday: null, //课次周一日期
      defaultValue: {
        //抽屉的课次基本信息
        course: '课程名称',
        class_time: '--',
        teachers: [],
        class_name: '--',
        classroom: '未设置',
        content: '--',
        class_start_time: '-',
        class_end_time: '-',
        type: 'call',
      },
      studentList: [], //抽屉的表格
      //编辑课次弹窗的信息
      labelCol: { span: 4 },
      wrapperCol: { span: 20 },
      editModal: false,
      modalLoading: false,
      changeClassForm: {
        //编辑课次信息弹框的数据
        start_time: null,
        teacher_ids: [],
        classroom_id: null,
        content: '',
        class_time_id: '',
        course: null,
        class_id: null,
        student_ids: [],
      },
      changeClassRules: {
        class_time_id: [{ required: true, message: '请选择上课时间', trigger: 'change' }],
        start_time: [{ required: true, message: '请选择上课日期', trigger: 'change' }],
      },
      //下拉菜单
      classroomList: [],
      classTimeSelect: [],
      teacherSelect: [],
      calledStudentColumns: [
        {
          title: '学员姓名',
          dataIndex: 'name',
          width: 100,
          ellipsis: true,
          scopedSlots: { customRender: 'name' },
        },
        {
          title: '手机号',
          dataIndex: 'main_phone',
          width: 120,
          scopedSlots: { customRender: 'phone' },
        },
        {
          title: '到课状态',
          dataIndex: 'call_type',
          ellipsis: true,
          width: 90,
          align: 'right',
          scopedSlots: { customRender: 'call_type' },
        },
        {
          title: '扣课时',
          dataIndex: 'deduction',
          scopedSlots: { customRender: 'deduction' },
        },
        {
          title: '备注',
          dataIndex: 'remarks',
          ellipsis: true,
          width: 150,
          scopedSlots: { customRender: 'remarks' },
        },
      ],
      studentColumns: [
        {
          title: '学员姓名',
          dataIndex: 'name',
          width: 150,
          ellipsis: true,
          scopedSlots: { customRender: 'name' },
        },
        {
          title: '手机号',
          dataIndex: 'main_phone',
          ellipsis: true,
        },
        {
          title: '消耗课程',
          dataIndex: 'course_name',
          ellipsis: true,
        },
        {
          title: '剩余时间/天数',
          ellipsis: true,
          scopedSlots: {
            customRender: 'surplusClassHour',
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 80,
          ellipsis: true,
          scopedSlots: { customRender: 'action' },
        },
      ],
      //临时学员
      linshiStudentModel: false,
      linshiKeywords: undefined,
      linshiColumns: [
        {
          title: '姓名',
          dataIndex: 'name',
          ellipsis: true,
        },
        {
          title: '手机号',
          dataIndex: 'main_phone',
          ellipsis: true,
        },
        {
          title: '报读课程',
          dataIndex: 'course_name',
          ellipsis: true,
        },
        {
          title: '剩余课时/天数',
          dataIndex: 'remain',
          ellipsis: true,
          scopedSlots: { customRender: 'remain' },
        },
      ],
      linshiData: [],
      selectedRowKeys: [],
      selectedRows: [],
      //学生调课
      changeClassModel: false,
      changeClassColumns: [
        {
          title: '班级名称',
          dataIndex: 'name',
          ellipsis: true,
          scopedSlots: { customRender: 'name' },
        },
        {
          title: '授课课程',
          dataIndex: 'course',
          ellipsis: true,
          scopedSlots: { customRender: 'course' },
        },
        {
          title: '上课时间',
          dataIndex: 'time',
          ellipsis: true,
          scopedSlots: { customRender: 'time' },
        },
        {
          title: '上课教室',
          dataIndex: 'classroom',
          ellipsis: true,
          scopedSlots: { customRender: 'classroom' },
        },
        {
          title: '上课老师',
          dataIndex: 'teacher',
          ellipsis: true,
          scopedSlots: { customRender: 'teacher' },
        },
        {
          title: '班级人数',
          dataIndex: 'peopleNum',
          ellipsis: true,
          scopedSlots: { customRender: 'peopleNum' },
        },
      ],
      changeClassLoadData: [],
      changeClassSelect: [],
      alreadyChoice: [],
      changeClassNowDate: moment(),
      changeClassMonday: '',
      changeClassStudent: null,
    }
  },
  watch: {
    visible(val) {
      if (val == false) {
        this.id = null
        this.date_Monday = null
        this.defaultValue = {
          course: '课程名称',
          class_time: '--',
          teachers: [],
          class_name: '--',
          classroom: '未设置',
          content: '--',
          class_start_time: '-',
          class_end_time: '-',
          type: 'call',
        }
        this.studentList = []
      }
    },
    linshiStudentModel(val) {
      this.linshiKeywords = undefined
      this.selectedRows = []
      this.selectedRowKeys = []
    },
    changeClassModel(val) {
      if (val == false) {
        this.changeClassNowDate = moment()
        this.changeClassLoadData = []
        this.changeClassSelect = []
        this.alreadyChoice = []
        this.changeClassMonday = this.getMonday(moment(this.changeClassNowDate).format('YYYY-MM-DD'))
        this.changeClassStudent = null
      }
    },
  },
  computed: {
    changeClassRowSelection() {
      const { selectedRowKeys } = this
      return {
        type: 'radio',
        selectedRowKeys: this.changeClassSelect,
        onChange: this.select,
        hideDefaultSelections: true,
      }
    },
  },
  created() {
    // this.getSelect()
  },
  methods: {
    showDrawer(params) {
      this.loading = true
      let data = JSON.parse(JSON.stringify(params))
      this.id = data.id
      this.type = data.type
      this.time = data.time
      this.class_id = data.class_id
      this.date_Monday = this.getMonday(data.time) //获取周一 后台要用
      this.changeClassForm.class_id = data.class_id
      this.changeClassForm.student_ids = data.student_ids
      this.changeClassForm.times = {
        start_time: data.class_start_time,
        end_time: data.class_end_time,
      }
      lessonInfo({
        id: this.id,
        start_time: this.time,
        type: this.type,
      })
        .then((res) => {
          this.defaultValue = res
          let obj = JSON.parse(JSON.stringify(res))
          this.getForm(obj)
          this.scheduleType = obj.schedule_type
          this.visible = true
        })
        .catch((err) => {
          this.$message.error(err.data.message || '获取课次详情失败')
        })
        .finally(() => {
          this.defaultValue.type = this.type
          this.loading = false
          if (this.type == 'call') {
            newTakeNameInfo({ pageSize: 999999 }, this.id).then((result) => {
              this.studentList = result.data
            })
          } else {
            takeNameLinshiStudent(
              {
                attend_class_day: this.time,
              },
              this.id
            ).then((result) => {
              this.studentList = result.data
            })
          }
        })
    },
    getDefaultValue() {
      this.loading = true
      lessonInfo({
        id: this.id,
        start_time: this.time,
        type: this.type,
      })
        .then((res) => {
          this.defaultValue = res
          let obj = JSON.parse(JSON.stringify(res))
          this.scheduleType = obj.schedule_type
          this.getForm(obj)
        })
        .catch((err) => {
          this.$message.error(err.data.message || '获取课次详情失败')
        })
        .finally(() => {
          this.defaultValue.type = this.type
          this.loading = false
          if (this.type == 'call') {
            // /students-in-class
            newTakeNameInfo({ pageSize: 999999 }, this.id).then((result) => {
              this.studentList = result.data
            })
          } else {
            // /default-call-student
            takeNameLinshiStudent({ attend_class_day: this.time }, this.id).then((result) => {
              this.studentList = result.data
            })
          }
        })
    },
    getSelect() {
      classroomList({ pageSize: 999999, school_id: this.$store.state.user.school }).then((res) => {
        this.classroomList = res.data
      }),
        classTimeList({ pageSize: 999999, school_id: this.$store.state.user.school }).then((res) => {
          this.classTimeSelect = res.data
        })
      teacherList({ school_id: this.$store.state.user.school, pageSize: 999999999 }).then((res) => {
        this.teacherSelect = res.data
      })
    },
    del() {
      let that = this
      this.$confirm({
        title: '提示：删除课次?',
        content: '确认删除本课次？',
        okText: '确认',
        okType: 'primary',
        cancelText: '删除',
        onOk() {
          delAdjust({ id: that.id, date: that.date_Monday })
            .then((res) => {
              that.$message.success('删除成功')
              that.visible = false
              that.$emit('deleteLesson')
            })
            .catch((err) => {
              that.$message.error(err.data.message || '删除失败了，请稍后重试~')
            })
        },
        onCancel() {},
      })
    },
    getForm(data) {
      // console.log(data, '接口数据')
      this.changeClassForm.teacher_ids = data.teacher_ids
      this.changeClassForm.classroom_id = data.classroom_id || null
      this.changeClassForm.content = data.content
      this.changeClassForm.class_time_id = undefined
      this.classTimeSelect.forEach((val) => {
        if (val.start_time == data.class_start_time && val.end_time == data.class_end_time) {
          this.changeClassForm.class_time_id = val.id
        }
      })
      this.changeClassForm.course = data.course
      this.changeClassForm.start_time = moment(this.time)
    },
    clickEdit(){
      this.editModal=true
      this.getSelect()
    },
    handleOk(formName) {
      //编辑信息的提交
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.modalLoading = true
          let params = JSON.parse(JSON.stringify(this.changeClassForm))
          this.classTimeSelect.forEach((val) => {
            if (val.id == params.class_time_id) {
              params.times = val
            }
          })
          params.start_time = moment(params.start_time).format('YYYY-MM-DD')
          params.school_id = this.$store.state.user.school
          params.date = this.date_Monday
          params.schedules_id = this.id
          mixStudents(params)
            .then((res) => {
              this.$message.success('编辑成功')
              this.editModal = false
              this.visible = false
              this.$emit('deleteLesson')
            })
            .catch((err) => {
              this.$message.error(err.data.message)
            })
            .finally(() => {
              this.modalLoading = false
            })
        } else {
          return false
        }
      })
    },
    getMonday(date) {
      //获取周一日期
      let dd = new Date(date)
      let week = dd.getDay()
      let minus = week ? week - 1 : 6
      dd.setDate(dd.getDate() - minus)
      let y = dd.getFullYear()
      let m = dd.getMonth() + 1
      let d = dd.getDate()
      return y + '-' + m + '-' + d
    },
    getLinshiStudent(data) {
      this.loading = true
      addLinshiStudentList(
        {
          keywords: this.linshiKeywords,
          schedules_id: data,
          pageSize: 9999999999,
        },
        this.class_id
      )
        .then((res) => {
          this.linshiData = res.data
          this.linshiStudentModel = true
        })
        .catch((err) => {
          this.$message.error(err.data.message || '获取临时学员列表失败~')
        })
        .finally(() => {
          this.loading = false
        })
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    //添加临时学员
    addStudent() {
      if (this.selectedRows.length == 0) {
        this.$message.error('请选择临时学员')
        return
      }
      let params = JSON.parse(JSON.stringify(this.changeClassForm))
      params.start_time = moment(params.start_time).format('YYYY-MM-DD')
      params.school_id = this.$store.state.user.school
      params.date = this.date_Monday
      params.schedules_id = this.id
      let arr = {}
      this.selectedRows.forEach((val) => {
        let key=val.id
        let value = {
          type: 1,
        }
        arr[key] = value
      })
      params.student_ids = arr
      mixStudents(params)
        .then((res) => {
          this.$message.success('添加临时学员成功')
          this.linshiStudentModel = false
          this.visible = false
          this.$emit('deleteLesson')
        })
        .catch((err) => {
          this.$message.error(err.data.message)
        })
    },
    //移除临时学员
    remove(e) {
      let that = this
      if (e.pivot_id) {
        this.$confirm({
          title: '移除学员?',
          content: '确定将' + e.name + '从该课次移出',
          okText: '确定',
          okType: 'primary',
          cancelText: '取消',
          onOk() {
            that.loading = true
            removeStudent({
              pivot_id: e.pivot_id,
            })
              .then((res) => {
                that.$message.success('移出学员' + e.name + '成功')
                that.visible = false
                that.$emit('deleteLesson')
              })
              .catch((err) => {
                that.$message.error(err.data.message || '移出学员' + e.name + '失败')
              })
              .finally(() => {
                that.loading = false
              })
          },
          onCancel() {},
        })
      }
    },
    //调课
    showChangeClass(e) {
      this.loading = true
      this.changeClassNowDate = moment()
      this.changeClassStudent = e
      changeClassTable({
        student_id: e.id,
        schedules_id: this.id,
        date: this.getMonday(moment(this.changeClassNowDate).format('YYYY-MM-DD')),
      })
        .then((res) => {
          this.changeClassLoadData = res.data
          this.changeClassModel = true
        })
        .catch((err) => {
          this.$message.error(err.data.message || '获取调课数据失败')
        })
        .finally(() => {
          this.loading = false
        })
    },
    changeClass() {
      if (this.alreadyChoice.length == 0) {
        this.$message.error('请选择调入的课次~')
        return
      }
      this.loading = true
      sureChangeClass({
        school_id: this.$store.state.user.school,
        class_id: this.class_id,
        student_id: this.changeClassStudent.id,
        from_data: {
          schedules_id: this.id,
          date: this.date_Monday,
          start_time: this.time,
        },
        to_data: {
          schedules_id: this.alreadyChoice[0].id,
          date: this.getMonday(moment(this.changeClassNowDate).format('YYYY-MM-DD')),
          start_time: this.$options.filters['dayjs'](this.alreadyChoice[0].date),
        },
      })
        .then((res) => {
          this.$message.success('调课成功')
          this.changeClassModel = false
          this.visible = false
          this.$emit('deleteLesson')
        })
        .catch((err) => {
          this.$message.error(err.data.message)
        })
        .finally(() => {
          this.loading = false
        })
    },
    changeNowDate(date, dateString) {
      this.$forceUpdate()
      this.changeClassMonday = this.getMonday(dateString)
      changeClassTable({
        student_id: this.changeClassStudent.id,
        schedules_id: this.id,
        date: this.getMonday(moment(this.changeClassNowDate).format('YYYY-MM-DD')),
      })
        .then((res) => {
          this.changeClassLoadData = res.data
          this.changeClassModel = true
        })
        .catch((err) => {
          this.$message.error(err.data.message || '获取调课数据失败')
        })
        .finally(() => {
          this.loading = false
        })
    },
    select(a, b) {
      this.changeClassSelect = a
      this.alreadyChoice = b
    },
    last7day(e) {
      let data = e.subtract('days', 7).format('YYYY-MM-DD')
      this.changeClassNowDate = moment(data)
      this.changeNowDate(this.changeClassNowDate, data)
      this.$forceUpdate()
    },
    next7day(e) {
      let data = e.subtract('days', -7).format('YYYY-MM-DD')
      this.changeClassNowDate = moment(data)
      this.changeNowDate(this.changeClassNowDate, data)
      this.$forceUpdate()
    },
    getNowWeek() {
      let data = moment().format('YYYY-MM-DD')
      this.changeClassNowDate = moment(data)
      this.changeNowDate(this.changeClassNowDate, data)
      this.$forceUpdate()
    },
  },
}
</script>

<style lang="less" scoped>
.classTitle {
  display: flex;
  justify-content: space-between;
  font-size: 16px;
}
.stepTitle {
  margin-bottom: 20px;
  .title {
    font-size: 16px;
  }
}
</style>